﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.Entity;
using System.Text.RegularExpressions;
using Estimate.Common.DBFieldTypes;

namespace Estimate.Common.Estimate
{

    //public enum DeviationDirection { East, Sourh, West, North, EastSouth, SouthWest, WestNorth, EastNorth }

    //public enum DeviationKind
    //{
    //    Ignore,   //忽略，不处理。全是ND时
    //    NoDeviation, //无偏差
    //    AllEmpty, //纯空报
    //    AllLack, //纯漏报
    //    Deviation,  //有偏差
    //    Reverse  //反向
    //}

    //public enum DeviationLevel
    //{
    //    Big,    //预报量级偏大
    //    Small,   //预报量级偏小
    //    Normal,   //普通
    //}

    /// <summary>
    /// 根据预报的结果，获得预报的误差方向
    /// </summary>
    public class WaterForecasteDeviationResult
    {

        public DeviationKind kind = DeviationKind.Ignore;

        public float X = 0f;

        public float Y = 0f;

        public int LackCount = 0;

        public int EmptyCount = 0;

        public WaterForecasteDeviationResult()
        {
 
        }

        //给定偏差类型
        public WaterForecasteDeviationResult(DeviationKind k)
        {
            kind = k;
        }

        /// <summary>
        /// 设置偏差结果
        /// </summary>
        /// <param name="x">x轴方向偏差</param>
        /// <param name="y">y轴方向偏差</param>
        /// <param name="lackCount">缺少数量</param>
        /// <param name="emptyCount">空格数量</param>
        public WaterForecasteDeviationResult(float x,float y,int lackCount, int emptyCount)
        {
            X = x;
            Y = y;
            LackCount = lackCount;
            EmptyCount = emptyCount;
            kind = DeviationKind.Deviation;
        }

        public DeviationDirection Direction
        {
            get
            {
                double angle = Math.Atan2(this.Y, this.X) * (180 / Math.PI);
                if (angle < 0)
                    angle += 360;

                if (angle >= 0 && angle < 22.5)
                    return DeviationDirection.East;
                else if (angle >= 22.5 && angle < 67.5)
                    return DeviationDirection.EastNorth;
                else if (angle >= 67.5 && angle < 112.5)
                    return DeviationDirection.North;
                else if (angle >= 112.5 && angle < 157.5)
                    return DeviationDirection.WestNorth;
                else if (angle >= 157.5 && angle < 202.5)
                    return DeviationDirection.West;
                else if (angle >= 202.5 && angle < 247.5)
                    return DeviationDirection.SouthWest;
                else if (angle >= 247.5 && angle < 292.5)
                    return DeviationDirection.Sourh;
                else if (angle >= 292.5 && angle < 337.5)
                    return DeviationDirection.EastSouth;
                else
                    return DeviationDirection.East;
            }
        }

        //预报偏大偏小
        public DeviationLevel Level
        {
            get
            {
                if (this.EmptyCount > this.LackCount)
                    return DeviationLevel.Big;
                else if (this.LackCount > this.EmptyCount)
                    return DeviationLevel.Small;
                else
                    return DeviationLevel.Normal;
            }
        }

        public static WaterForecasteDeviationResult operator +(WaterForecasteDeviationResult d1, WaterForecasteDeviationResult d2)
        {
            return new WaterForecasteDeviationResult( d1.X + d2.X, d1.Y + d2.Y, d1.LackCount + d2.LackCount, d1.EmptyCount + d2.EmptyCount);
        }

        public override string ToString()
        {    
            return string.Format("{0},{1},{2},{3},{4}", new object[]{
                        this.kind,
                        this.X,
                        this.Y,
                        this.LackCount,
                        this.EmptyCount
                    });
        }

        public void FromString(string value)
        {
            string[] valueArray = Regex.Split(value, @"\,");
            kind = (DeviationKind)Enum.Parse(typeof(DeviationKind), valueArray[0]);
            X = Convert.ToSingle(valueArray[1]);
            Y = Convert.ToSingle(valueArray[2]);
            LackCount = Convert.ToInt32(valueArray[3]);
            EmptyCount = Convert.ToInt32(valueArray[4]);
        }

    }
}
